import {ASSET_TYPES} from "../../shared/constants";
import {isDef, isPlainObject} from "../../shared/util";

/**
 * 初始化资源注册器
 * @param {Reactive} Reactive
 */
export function initAssetsRegister (Reactive) {
    // 遍历每一种资源，并进行注册
    ASSET_TYPES.forEach((type) => {
        Reactive[type] = function (name, definition) {
            if (!definition) {
                return Reactive[`${type}s`][name];
            }
            else {
                if (type === 'component' && isPlainObject(definition)) {
                    definition.name = definition.name || name;
                    definition = this.options._base.extend(definition);
                }
                !isDef(Reactive[`${type}s`]) ? Reactive[`${type}s`] = {} : undefined;
                Reactive[`${type}s`][name] = definition;
                return definition;
            }
        }
    });
}


/**
 * 获取解析资源
 * @param {String} type
 * @param {String} name
 * @return {Object}
 */
export function resolveAssets (type, name) {
    // 名称处理
    // todo
    // 获取资源
    const asset = Reactive[type+'s'][name];
    return isDef(asset) ? asset : null;
}
